package com.lovo.cinema.controller;

import com.lovo.cinema.model.Movies;
import com.lovo.utile.DTO.MovieDTO;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class CinemaController {

    @PostMapping("buy")
    public String buy(@RequestBody MovieDTO movieDTO){
        List<Movies> moviesList=new ArrayList<>();
        Movies movies=new Movies("八佰",10,30);
        Movies movies1=new Movies("除暴",5,30);
        Movies movies2=new Movies("喜羊羊",6,20);
        moviesList.add(movies);
        moviesList.add(movies1);
        moviesList.add(movies2);

        String name=movieDTO.getName();
        int num=movieDTO.getNum();
        String info="";
        int code=265656;
        for (Movies mo :moviesList){
//            System.out.println(mo.getName()+""+mo.getNum()+"da"+name);
            if (mo.getName().equals(name)&&mo.getNum()>0){
                if (num>mo.getNum()){
                    info="剩余票数不足";
                }else {
                    mo.setNum(mo.getNum()-1);
                    info="取票编码:"+code+1+";总价格:"+mo.getPrice()*num;
                }
            }else if (mo.getName().equals(name)&&mo.getNum()<=0){
                info="电影票已售完";
            }
//            if (!(mo.getName().equals(name))){
//                System.out.println(mo.getName()+"zz"+name);
//                info="该电影不存在";
//            }
        }
        return info;
    }
}
